package xkp.lesson.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//@Component
//@Aspect
public class Log {

    @Pointcut("execution(* xkp.lesson.dao.*.*(..))")
    public void expre1(){}

    @Pointcut("execution(* xkp.lesson.dao.*.insert*(..))")
    public void expre2(){}

    @Before(value = "expre1()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println("方法执行前....");
    }

    @After(value = "expre2()")
    public void beforeMethod2(JoinPoint joinPoint){
       /* System.out.println(joinPoint.getSignature().getName());
        //System.out.println(joinPoint.getArgs());
        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.toString(args));*/
        System.out.println("方法执行前2....");
    }
    public void afterMethod(JoinPoint point){
        System.out.println("方法执行后...");
    }

    @AfterReturning(value = "expre1()", returning = "returnValue")
    public void afterRetuen(JoinPoint point,Object returnValue){
        System.out.println("获取方法的返回值");
        System.out.println(returnValue);
    }

    @AfterThrowing(value = "expre2()",throwing = "ex")
    public void afterThrow(JoinPoint joinPoint,Exception ex){
        System.out.println("异常通知");
        System.out.println(ex.getMessage());
    }

    public void around(ProceedingJoinPoint joinPoint){
        System.out.println("前置通知");
        try {
            Object returnValue =  joinPoint.proceed();
            System.out.println("返回通知:"+returnValue);

        } catch (Throwable throwable) {
            System.out.println("异常通知");
            throwable.printStackTrace();
        }finally {
            System.out.println("最终通知");
        }
    }
}
